/*
 * comm_callback.c
 * 通讯指令
 *  Created on: 2021年9月25日
 *      Author: samuel
 */


/* Includes ------------------------------------------------------------------*/
//#include "comm.h"
#include "version.h"
#include "cmd_process.h"
#include "ParmManage.h"
#include "w5500_fun.h"
/* Private defines -----------------------------------------------------------*/



/* Public enum    -----------------------------------------------------------*/


/* Public Struct  -----------------------------------------------------------*/


/* Private Variable  ---------------------------------------------------------*/
//Pointer

//Array

//Const

/* Private function prototypes -----------------------------------------------*/



/***********************************************************************************
 * @brief
 * ex:
 * @par    None
 * @retval None
 **********************************************************************************/
ack_packet_t *dhcp_switch_callback   ( user_packet_t * user )
{
    ack_packet_t *ack;
    if(user->style == CMD_SET)    //设置
    {
        // 用户数据处理
    	PPItemIWrite(PP_DHCP_SWITCH_CDT, &user->data[0], 1);
        // 应答返回数据
        ack = Ack_Malloc(1);
        ack->data[0] = 0x00;
    }
    else if(user->style == CMD_READ)       //读取
    {
        // 应答返回数据
        ack = Ack_Malloc(1);
        PPItemRead(PP_DHCP_SWITCH_CDT, &ack->data[0], 1);
    }

	return ack;
}

/***********************************************************************************
 * @brief
 * ex:
 * @par    None
 * @retval None
 **********************************************************************************/
ack_packet_t *local_ip_callback      ( user_packet_t * user )
{
    ack_packet_t *ack;
    uint32_t change_flag = 0;
    
    if(user->style == CMD_SET)    //设置
    {
        // 用户数据处理
    	PPItemIWrite(PP_LOCAL_IP_CDT, &user->data[0], 4);
        
        //是否修改标志
        change_flag = 0xAA;
        PPItemIWrite(PP_CHANGE_FLAG, (uint8_t *)(&change_flag), 4);

        // 应答返回数据
        ack = Ack_Malloc(1);
        ack->data[0] = 0x00;
    }
    else if(user->style == CMD_READ)       //读取
    {
        // 应答返回数据
        ack = Ack_Malloc(4);
        PPItemRead(PP_LOCAL_IP_CDT, &ack->data[0], 4);
    }
	return ack;
}

/***********************************************************************************
 * @brief
 * ex:
 * @par    None
 * @retval None
 **********************************************************************************/
ack_packet_t *local_port_callback    ( user_packet_t * user )
{
    ack_packet_t *ack;
    if(user->style == CMD_SET)    //设置
    {
        // 用户数据处理
    	PPItemIWrite(PP_LOCAL_PORT_CDT, &user->data[0], 2);
        // 应答返回数据
        ack = Ack_Malloc(1);
        ack->data[0] = 0x00;
    }
    else if(user->style == CMD_READ)       //读取
    {
        // 应答返回数据
        ack = Ack_Malloc(2);
        PPItemRead(PP_LOCAL_PORT_CDT, &ack->data[0], 2);
    }
	return ack;
}

/***********************************************************************************
 * @brief
 * ex:
 * @par    None
 * @retval None
 **********************************************************************************/
ack_packet_t *gateway_callback       ( user_packet_t * user )
{
    ack_packet_t *ack;
    if(user->style == CMD_SET)    //设置
    {
        // 用户数据处理
    	PPItemIWrite(PP_WAY_CDT, &user->data[0], 4);
        // 应答返回数据
        ack = Ack_Malloc(1);
        ack->data[0] = 0x00;
    }
    else if(user->style == CMD_READ)       //读取
    {
        // 应答返回数据
        ack = Ack_Malloc(4);
        PPItemRead(PP_WAY_CDT, &ack->data[0], 4);
    }
	return ack;
}

/***********************************************************************************
 * @brief
 * ex:
 * @par    None
 * @retval None
 **********************************************************************************/
ack_packet_t *netmark_callback       ( user_packet_t * user )
{
    ack_packet_t *ack;
    if(user->style == CMD_SET)    //设置
    {
        // 用户数据处理
    	PPItemIWrite(PP_SUB_MASK_CDT, &user->data[0], 4);
        // 应答返回数据
        ack = Ack_Malloc(1);
        ack->data[0] = 0x00;
    }
    else if(user->style == CMD_READ)       //读取
    {
        // 应答返回数据
        ack = Ack_Malloc(4);
        PPItemRead(PP_SUB_MASK_CDT, &ack->data[0], 4);
    }
	return ack;
}

/***********************************************************************************
 * @brief
 * ex:
 * @par    None
 * @retval None
 **********************************************************************************/
ack_packet_t *mac_callback           ( user_packet_t * user )
{
    ack_packet_t *ack;
    uint32_t change_flag = 0;
    
    if(user->style == CMD_SET)    //设置
    {
        // 用户数据处理
    	PPItemIWrite(PP_MAC_CDT, &user->data[0], 6);
           
        // 应答返回数据
        ack = Ack_Malloc(1);
        ack->data[0] = 0x00;
    }
    else if(user->style == CMD_READ)       //读取
    {
        // 应答返回数据
        ack = Ack_Malloc(6);
        PPItemRead(PP_MAC_CDT, &ack->data[0], 6);
    }
	return ack;
}

/***********************************************************************************
 * @brief
 * ex:
 * @par    None
 * @retval None
 **********************************************************************************/
ack_packet_t *search_device_callback ( user_packet_t * user )
{
    ack_packet_t *ack;

    int i,j,k,asc_flag;
	// 应答返回数据
	ack = Ack_Malloc(44);
	// ip
	PPItemRead(PP_LOCAL_IP_CDT,   &ack->data[0], 4);
	// port
	PPItemRead(PP_LOCAL_PORT_CDT, &ack->data[4], 2);
	// 项目代号
	j=2+4;
	asc_flag=0;
	for(i=0; i<16; i++,j++)
	{
		if(APPNAME[i] == '-'){
			asc_flag++;
		}
		if(asc_flag >= 2){
			ack->data[j] = 0x00;
		}
		else{
			ack->data[j] = APPNAME[i];
		}
	}
	// 电路名称
	i = 0;
	j = 2+4+16;
	asc_flag = 0;
	for(k=0; k<128; k++){
		if(APPNAME[k] == '-'){
			asc_flag++;
		}
		if(asc_flag >= 2)
		{
			asc_flag = 0;
			k += 1;
			break;
		}
	}
	for(i=k; i<k+16; i++,j++){
		if(APPNAME[i] == '\0'){
			asc_flag++;
		}
		if(asc_flag >= 1){
			ack->data[j] = 0x00;
		}
		else{
			ack->data[j] = APPNAME[i];
		}
	}
	// MAC
	PPItemRead(PP_MAC_CDT, &ack->data[j], 6);
	return ack;
}

/***********************************************************************************
 * @brief
 * ex:
 * @par    None
 * @retval None
 **********************************************************************************/
ack_packet_t *network_par_callback   ( user_packet_t * user )
{
    ack_packet_t *ack;
    
    if(user->style == CMD_SET)    //设置
    {
        // 用户数据处理
    	PPItemIWrite(PP_LOCAL_IP_CDT,   &user->data[0],  4);
    	PPItemIWrite(PP_LOCAL_PORT_CDT, &user->data[4],  2);
    	PPItemIWrite(PP_WAY_CDT,        &user->data[6],  4);
    	PPItemIWrite(PP_SUB_MASK_CDT,   &user->data[10], 4);
        // 应答返回数据
        ack = Ack_Malloc(1);
        ack->data[0] = 0x00;
    }
    else if(user->style == CMD_READ)       //读取
    {
        // 应答返回数据
        ack = Ack_Malloc(14);
        PPItemRead(PP_LOCAL_IP_CDT, 	&ack->data[0],  4);
        PPItemRead(PP_LOCAL_PORT_CDT, 	&ack->data[4],  2);
        PPItemRead(PP_WAY_CDT, 			&ack->data[6],  4);
        PPItemRead(PP_SUB_MASK_CDT,		&ack->data[10], 4);
    }
	return ack;
}

/***********************************************************************************
 * @brief
 * ex:
 * @par    None
 * @retval None
 **********************************************************************************/
//ack_packet_t *update_par_callback    ( user_packet_t * user )
//{
//    ack_packet_t *ack;
//	// 用户数据处理
//    comm_local_param(COMM_TCP);
//    // 应答返回数据
//    ack = Ack_Malloc(1);
//	ack->data[0] = 0x00;
//    // 系统复位
//    ack->system_reset_flag = 1;

//	return ack;
//}





/******************* (C) COPYRIGHT 2021 CIQTEK Samuel *****END OF FILE****/

